import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QListWidget, QVBoxLayout, QHBoxLayout

class CompoundWidget(QWidget):
    def __init__(self, text, parent=None):
        super().__init__(parent)
        self.text = text
        self.init_ui()

    def init_ui(self):
        # 创建一个按钮
        self.button = QPushButton(f"{self.text} 按钮", self)
        self.button.clicked.connect(self.on_button_click)

        # 创建一个列表框
        self.list_widget = QListWidget(self)
        self.list_widget.itemClicked.connect(self.on_list_item_click)

        # 向列表框添加一些内容
        for i in range(5):
            self.list_widget.addItem(f"{self.text} 列表项 {i+1}")

        # 创建布局并添加控件
        layout = QVBoxLayout(self)
        layout.addWidget(self.button)
        layout.addWidget(self.list_widget)

    def on_button_click(self):
        print(f"{self.text} 按钮被点击了")

    def on_list_item_click(self, item):
        print(f"{self.text} 选中的列表项：{item.text()}")
